﻿// <copyright file="MyOpenSpacePerson.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-24</date>
// <summary>MyOpenSpace.Person class</summary>

namespace MyOpenSpace
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Browser;
    using System.Windows.Media.Imaging;
    using Synergist;
    
    /// <summary>
    /// MyOpenSpace Person
    /// </summary>
    public class Person
    {
        /// <summary>
        /// Initializes a new instance of the Person class.
        /// </summary>
        public Person()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Person class.
        /// </summary>
        /// <param name="scriptObject">a opensocial.person JavaScript object</param>
        public Person(ScriptObject scriptObject) :
            this(new opensocial.Person(scriptObject))
        {
        }

        /// <summary>
        /// Initializes a new instance of the Person class.
        /// </summary>
        /// <param name="person">the opensocial person</param>
        public Person(opensocial.Person person)
        {
            this.AboutMe        = person.getField("opensocial.Person.Field.ABOUT_ME") as string;
            object age = person.getField("opensocial.Person.Field.AGE");

            if (age != null && age.GetType() == typeof(double))
            {
                this.Age = (double) age;
            }

            var bodyType = person.getField("opensocial.Person.Field.BODY_TYPE") as ScriptObject;
            this.BodyType = new opensocial.BodyType(bodyType);
            this.Books          = person.GetStringArrayField("opensocial.Person.Field.BOOKS");
            this.Children       = person.getField("opensocial.Person.Field.CHILDREN") as string;
            this.CurrentLocation = new opensocial.Address(person.getField("opensocial.Person.Field.CURRENT_LOCATION") as ScriptObject);
            this.DateOfBirth    = person.GetDateField("opensocial.Person.Field.DATE_OF_BIRTH");
            this.DisplayName = person.getDisplayName();
            this.Drinker = new opensocial.Enum(person.ScriptObject.getField("opensocial.Person.Field.DRINKER") as ScriptObject);
            this.Ethnicity = person.ScriptObject.getField("opensocial.Person.Field.ETHNICITY") as string;
            this.Gender = new opensocial.Enum(person.ScriptObject.getField("opensocial.Person.Field.GENDER") as ScriptObject);
            var hasApp = person.ScriptObject.getField("opensocial.Person.Field.HAS_APP");

            if (hasApp != null)
            {
                this.HasApp = (bool)hasApp;
            }

            this.Heroes         = person.GetStringArrayField("opensocial.Person.Field.HEROES");
            this.ID             = person.getField("opensocial.Person.Field.ID") as string;
            this.Interests      = person.GetStringArrayField("opensocial.Person.Field.INTERESTS");
            this.Jobs = from item in person.ScriptObject.GetFieldCollection("opensocial.Person.Field.JOBS")
                        select new opensocial.Organization(item);
            this.LookingFor = new opensocial.Enum(person.ScriptObject.getField("opensocial.Person.Field.LOOKING_FOR") as ScriptObject);
            this.LargeImage     = person.GetImageField("MyOpenSpace.Person.Field.LARGE_IMAGE");
            this.MediumImage    = person.GetImageField("MyOpenSpace.Person.Field.MEDIUM_IMAGE");
            this.Movies         = person.GetStringArrayField("opensocial.Person.Field.MOVIES");
            this.Music          = person.GetStringArrayField("opensocial.Person.Field.MUSIC");
            this.Name           = new opensocial.Name(person.getField("opensocial.Person.Field.NAME") as ScriptObject);
            this.NetworkPresence = new opensocial.Enum(person.getField("opensocial.Person.Field.NETWORK_PRESENCE") as ScriptObject);
            this.Nickname       = person.getField("opensocial.Person.Field.NICKNAME") as string;
            this.PhoneNumbers   = person.GetStringArrayField("opensocial.Person.Field.PHONE_NUMBERS");
            this.ProfileSong = new opensocial.Url(person.ScriptObject.getField("opensocial.Person.Field.PROFILE_SONG") as ScriptObject);
            this.ProfileUrl     = person.GetUriField("opensocial.Person.Field.PROFILE_URL");
            this.ProfileVideo   = person.GetUriField("opensocial.Person.Field.PROFILE_VIDEO");
            this.RelationshipStatus = person.getField("opensocial.Person.Field.RELATIONSHIP_STATUS") as string;
            this.Religion = person.getField("opensocial.Person.Field.RELIGION") as string;
            this.SexualOrientation = person.getField("opensocial.Person.Field.SEXUAL_ORIENTATION") as string;
            this.Smoker = new opensocial.Enum(person.getField("opensocial.Person.Field.SMOKER") as ScriptObject);
            this.Status         = person.getField("opensocial.Person.Field.STATUS") as string;
            this.ThumbnailImage = person.ScriptObject.GetImageField("opensocial.Person.Field.THUMBNAIL_URL");
            this.TVShows = person.ScriptObject.GetStringArrayField("opensocial.Person.Field.TV_SHOWS");
            this.Urls = from item in person.ScriptObject.GetFieldCollection("opensocial.Person.Field.URLS")
                        select new opensocial.Url(item);
        }

        /// <summary>
        /// Gets or sets a detail about me
        /// </summary>
        public string AboutMe { get; set; }

        /// <summary>
        /// Gets or sets the person's age
        /// </summary>
        public double? Age { get; set; }

        /// <summary>
        /// Gets or sets the person's body type
        /// </summary>
        /// <remarks>opensocial.BodyType</remarks>
        public opensocial.BodyType BodyType { get; set; }

        /// <summary>
        /// Gets or sets the ID
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ID", Justification = "Mirroring OpenSocial API.")]
        public string ID { get; set; }

        /// <summary>
        /// Gets or sets the children
        /// </summary>
        public string Children { get; set; }

        /// <summary>
        /// Gets or sets the date of birth
        /// </summary>
        public DateTime? DateOfBirth { get; set; }

        /// <summary>
        /// Gets or sets the display name
        /// </summary>
        /// <remarks>from opensocial.Person.getDisplayName()</remarks>
        public string DisplayName { get; set; }

        /// <summary>
        /// Gets or sets the drinking status
        /// </summary>
        public opensocial.Enum Drinker { get; set; }

        /// <summary>
        /// Gets or sets the ethnicity
        /// </summary>
        public string Ethnicity { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the person has an app
        /// </summary>
        public bool HasApp { get; set; }

        /// <summary>
        /// Gets or sets the jobs
        /// </summary>
        public IEnumerable<opensocial.Organization> Jobs { get; set; }

        /// <summary>
        /// Gets or sets the social goals
        /// </summary>
        public opensocial.Enum LookingFor { get; set; }

        /// <summary>
        /// Gets or sets the large image
        /// </summary>
        public BitmapImage LargeImage { get; set; }

        /// <summary>
        /// Gets or sets the medium image
        /// </summary>
        public BitmapImage MediumImage { get; set; }

        /// <summary>
        /// Gets or sets the network presence
        /// </summary>
        public opensocial.Enum NetworkPresence { get; set; }

        /// <summary>
        /// Gets or sets the person's interestes
        /// </summary>
        public IEnumerable<string> Interests { get; set; }

        /// <summary>
        /// Gets or sets the person's favorite music
        /// </summary>
        public IEnumerable<string> Music { get; set; }

        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public opensocial.Name Name { get; set; }

        /// <summary>
        /// Gets or sets the person's nickname
        /// </summary>
        public string Nickname { get; set; }

        /// <summary>
        /// Gets or sets the thumbnail image
        /// </summary>
        public BitmapImage ThumbnailImage { get; set; }

        /// <summary>
        /// Gets or sets the TV Shows
        /// </summary>
        public IEnumerable<string> TVShows { get; set; }

        /// <summary>
        /// Gets or sets the URLs
        /// </summary>
        public IEnumerable<opensocial.Url> Urls { get; set; }

        /// <summary>
        /// Gets or sets the profile URL
        /// </summary>
        public Uri ProfileUrl { get; set; }

        /// <summary>
        /// Gets or sets the person's current location
        /// </summary>
        public opensocial.Address CurrentLocation { get; set; }

        /// <summary>
        /// Gets or sets the phone numbers
        /// </summary>
        public IEnumerable<string> PhoneNumbers { get; set; }

        /// <summary>
        /// Gets or sets the person's favorite books
        /// </summary>
        /// <remarks>in MySpace this will always be a single string element.</remarks>
        public IEnumerable<string> Books { get; set; }

        /// <summary>
        /// Gets or sets the person's gender
        /// </summary>
        public opensocial.Enum Gender { get; set; }

        /// <summary>
        /// Gets or sets the person's heroes
        /// </summary>
        /// <remarks>in MySpace this will always be a single string element.</remarks>
        public IEnumerable<string> Heroes { get; set; }

        /// <summary>
        /// Gets or sets the person's favorite movies
        /// </summary>
        /// <remarks>in MySpace this will always be a single string element.</remarks>
        public IEnumerable<string> Movies { get; set; }

        /// <summary>
        /// Gets or sets the status
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// Gets or sets the URL for the profile song
        /// </summary>
        public opensocial.Url ProfileSong { get; set; }

        /// <summary>
        /// Gets or sets the URL for the Profile video
        /// </summary>
        public Uri ProfileVideo { get; set; }

        /// <summary>
        /// Gets or sets the relationship status
        /// </summary>
        public string RelationshipStatus { get; set; }

        /// <summary>
        /// Gets or sets the religion
        /// </summary>
        public string Religion { get; set; }

        /// <summary>
        /// Gets or sets the sexual orientation
        /// </summary>
        public string SexualOrientation { get; set; }

        /// <summary>
        /// Gets or sets the smoking status
        /// </summary>
        public opensocial.Enum Smoker { get; set; }

        /// <summary>
        /// Gets the properties
        /// </summary>
        /// <returns>the non-null properties</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }
    }
}
